<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="姓名" prop="teacherName">
        <el-input
          v-model="queryParams.teacherName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="性别" prop="gender">
        <el-input
          v-model="queryParams.gender"
          placeholder="请输入性别"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="授课科目" prop="teachingcategoryName">
        <el-input
          v-model="queryParams.teachingcategoryName"
          placeholder="请输入授课科目"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最高学历" prop="education">
        <el-input
          v-model="queryParams.education"
          placeholder="请输入最高学历"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="时薪要求" prop="hourlyRate">
        <el-input
          v-model="queryParams.hourlyRate"
          placeholder="请输入时薪要求"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['profile:profile:add']"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['profile:profile:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['profile:profile:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['profile:profile:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="profileList" @selection-change="handleSelectionChange">
      <!-- 左侧固定列 -->
      <el-table-column type="selection" width="55" align="center" fixed="left" />
      <el-table-column label="序号" align="center" prop="teacherId" width="100" fixed="left" sortable/>
      <el-table-column label="教员姓名" align="center" prop="teacherName" width="120" fixed="left" />
      <el-table-column label="照片" align="center" prop="avatar" width="100" fixed="left">
        <template slot-scope="scope">
          <image-preview :src="scope.row.avatar" :width="50" :height="50"/>
        </template>
      </el-table-column>

      <!-- 中间可滑动的列 -->
      <el-table-column label="性别" align="center" prop="gender" />
      <el-table-column label="年龄" align="center" prop="age" />
      <el-table-column label="授课科目" align="center" prop="teachingcategoryName" />
      <el-table-column label="所在学校" align="center" prop="school" />
      <el-table-column label="所学专业" align="center" prop="major" />
      <el-table-column label="最高学历" align="center" prop="education" />
      <el-table-column label="资格证书" align="center" prop="certification" />
      <el-table-column label="教学经历" align="center" prop="teachingExp">
        <template slot-scope="scope">
          <div @click="showTeachingExp(scope.row.teachingExp)" style="cursor: pointer;">
            {{ scope.row.teachingExp ? (scope.row.teachingExp.length > 3 ? scope.row.teachingExp.substring(0, 3) + '...' : scope.row.teachingExp) : '-' }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="个人介绍" align="center" prop="introduction">
        <template slot-scope="scope">
          <div @click="showIntroduction(scope.row.introduction)" style="cursor: pointer;">
            {{ scope.row.introduction ? (scope.row.introduction.length > 3 ? scope.row.introduction.substring(0, 3) + '...' : scope.row.introduction) : '-' }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="联系方式" align="center" prop="phoneNumber" />
      <el-table-column label="电子邮箱" align="center" prop="email" />
      <el-table-column label="时薪要求" align="center" prop="hourlyRate" />

      <!-- 右侧固定列 -->
      <el-table-column label="审核状态" align="center" prop="status" fixed="right">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_teacher_application_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="120">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="viewDetails(scope.row)"
            v-hasPermi="['profile:profile:view']"
          >查看详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['profile:profile:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['profile:profile:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改教员信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="照片" prop="avatar">
          <image-upload v-model="form.avatar"/>
        </el-form-item>
        <el-form-item label="姓名" prop="teacherName">
          <el-input v-model="form.teacherName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-input v-model="form.gender" placeholder="请输入性别" />
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="form.age" placeholder="请输入年龄" />
        </el-form-item>
        <el-form-item label="授课科目" prop="teachingcategoryName">
          <el-input v-model="form.teachingcategoryName" placeholder="请输入授课科目" />
        </el-form-item>
        <el-form-item label="所在学校" prop="school">
          <el-input v-model="form.school" placeholder="请输入所在学校" />
        </el-form-item>
        <el-form-item label="所学专业" prop="major">
          <el-input v-model="form.major" placeholder="请输入所学专业" />
        </el-form-item>
        <el-form-item label="最高学历" prop="education">
          <el-input v-model="form.education" placeholder="请输入最高学历" />
        </el-form-item>
        <el-form-item label="资格证书" prop="certification">
          <el-input v-model="form.certification" placeholder="请输入资格证书" />
        </el-form-item>
        <el-form-item label="教学经历" prop="teachingExp">
          <el-input v-model="form.teachingExp" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="个人介绍" prop="introduction">
          <el-input v-model="form.introduction" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="联系方式" prop="phoneNumber">
          <el-input v-model="form.phoneNumber" placeholder="请输入联系方式" />
        </el-form-item>
        <el-form-item label="电子邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入电子邮箱" />
        </el-form-item>
        <el-form-item label="时薪要求" prop="hourlyRate">
          <el-input v-model="form.hourlyRate" placeholder="请输入时薪要求" />
        </el-form-item>

        <!-- 管理员手动添加，审核状态直接置为通过 -->
        <!-- <el-form-item label="审核状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.sys_teacher_application_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item> -->

        <!-- 管理员手动添加，审核状态直接置为通过 -->
        <el-form-item label="审核状态" prop="status">
          <el-radio-group v-model="form.status" disabled>  <!-- 禁用修改 -->
            <el-radio label="approved">审核通过</el-radio>  <!-- 直接写死 -->
          </el-radio-group>
        </el-form-item>

        <!-- 教员星级 -->
        <el-form-item label="教员星级" prop="rating">
          <el-rate
            v-model="form.rating"
            :max="5"
            :allow-half="false"
            :colors="['#99A9BF', '#F7BA2A', '#FF9900']"
            size="40px"
          ></el-rate>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="'个人介绍'" :visible.sync="dialogVisible" width="500px" :modal="true" :center="true">
      <p>{{ detailedIntroduction }}</p>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">关闭</el-button>
      </span>
    </el-dialog>

    <el-dialog :title="'教学经历'" :visible.sync="teachingExpDialogVisible" width="500px" :modal="true" :center="true">
      <p>{{ detailedTeachingExp }}</p>
      <span slot="footer" class="dialog-footer">
        <el-button @click="teachingExpDialogVisible = false">关闭</el-button>
      </span>
    </el-dialog>

    <el-dialog :visible.sync="detailDialogVisible" width="600px" title="教员详情">
      <div>
        <p><strong>教员姓名:</strong> {{ selectedTeacher.teacherName }}</p>
        <p><strong>性别:</strong> {{ selectedTeacher.gender }}</p>
        <p><strong>年龄:</strong> {{ selectedTeacher.age }}</p>
        <p><strong>授课科目:</strong> {{ selectedTeacher.teachingcategoryName }}</p>
        <p><strong>所在学校:</strong> {{ selectedTeacher.school }}</p>
        <p><strong>所学专业:</strong> {{ selectedTeacher.major }}</p>
        <p><strong>最高学历:</strong> {{ selectedTeacher.education }}</p>
        <p><strong>资格证书:</strong> {{ selectedTeacher.certification }}</p>
        <p><strong>教学经历:</strong> {{ selectedTeacher.teachingExp }}</p>
        <p><strong>个人介绍:</strong> {{ selectedTeacher.introduction }}</p>
        <p><strong>联系方式:</strong> {{ selectedTeacher.phoneNumber }}</p>
        <p><strong>电子邮箱:</strong> {{ selectedTeacher.email }}</p>
        <p><strong>期望时薪:</strong> {{ selectedTeacher.hourlyRate }}</p>
        <p><strong>教员星级:</strong> 
          <el-rate
            v-model="selectedTeacher.rating"
            disabled
            :max="5"
            :colors="['#99A9BF', '#F7BA2A', '#FF9900']"
          ></el-rate>
        </p>

      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="detailDialogVisible = false">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProfile, getProfile, delProfile, addProfile, updateProfile } from "@/api/profile/profile";
import { updateApplicationStatusByUserId } from "@/api/application/application";

export default {
  name: "Profile",
  dicts: ['sys_teacher_application_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 教员信息表格数据
      profileList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        teacherName: null,
        gender: null,
        teachingcategoryName: null,
        education: null,
        hourlyRate: null,
      },
      status: 'approved', // 默认通过(管理员手动添加)
      rating: 0, // 默认评分值为0
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        avatar: [
          { required: true, message: "照片不能为空", trigger: "blur" }
        ],
        teacherName: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        gender: [
          { required: true, message: "性别不能为空", trigger: "blur" }
        ],
        age: [
          { required: true, message: "年龄不能为空", trigger: "blur" }
        ],
        teachingcategoryName: [
          { required: true, message: "授课科目不能为空", trigger: "blur" }
        ],
        school: [
          { required: true, message: "所在学校不能为空", trigger: "blur" }
        ],
        major: [
          { required: true, message: "所学专业不能为空", trigger: "blur" }
        ],
        education: [
          { required: true, message: "最高学历不能为空", trigger: "blur" }
        ],
        certification: [
          { required: true, message: "资格证书不能为空", trigger: "blur" }
        ],
        teachingExp: [
          { required: true, message: "教学经历不能为空", trigger: "blur" }
        ],
        introduction: [
          { required: true, message: "个人介绍不能为空", trigger: "blur" }
        ],
        phoneNumber: [
          { required: true, message: "联系方式不能为空", trigger: "blur" }
        ],
        email: [
          { required: true, message: "电子邮箱不能为空", trigger: "blur" }
        ],
        hourlyRate: [
          { required: true, message: "时薪要求不能为空", trigger: "blur" }
        ],
        rating: [
          { required: true, message: "请选择教员星级", trigger: "blur" }
        ]
      },
      dialogVisible: false,
      detailedIntroduction: '',
      teachingExpDialogVisible: false,
      detailedTeachingExp: '',
      detailDialogVisible: false,
      selectedTeacher: {}
    };
  },
  created() {
    this.getList();
  },
  // 进入页面时获取列表数据
  beforeRouteEnter(to, from, next) {
  next((vm) => {
    vm.getList();
    next();
    });
  },
  methods: {
    /** 查询教员信息列表 */
    getList() {
      this.loading = true;
      listProfile(this.queryParams).then(response => {
        console.log('教员信息查询成功',response);
        
        this.profileList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        teacherId: null,
        avatar: null,
        teacherName: null,
        gender: null,
        age: null,
        teachingcategoryName: null,
        school: null,
        major: null,
        education: null,
        certification: null,
        teachingExp: null,
        introduction: null,
        phoneNumber: null,
        email: null,
        hourlyRate: null,
        status: 'approved'
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.teacherId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加教员信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const teacherId = row.teacherId || this.ids
      getProfile(teacherId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改教员信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.teacherId != null) {
            updateProfile(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProfile(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const teacherIds = row.teacherId || this.ids;
      this.$modal.confirm('是否确认删除教员信息编号为"' + teacherIds + '"的数据项？').then(function() {
        return delProfile(teacherIds);
      }).then((response) => {
        // 删除成功后，获取被删除教员的 userId
        const userId = row.userId;
        // 更新教员申请状态为 pending
        updateApplicationStatusByUserId(userId).then(() => {
          this.$modal.msgSuccess("删除成功，教员申请状态已重置为审核中");
        }).catch(() => {
          this.$modal.msgError("状态更新失败，请手动检查");
        });
        this.getList();
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('profile/profile/export', {
        ...this.queryParams
      }, `profile_${new Date().getTime()}.xlsx`)
    },
    showIntroduction(introduction) {
      this.detailedIntroduction = introduction;
      this.dialogVisible = true;
    },
    showTeachingExp(teachingExp) {
      this.detailedTeachingExp = teachingExp;
      this.teachingExpDialogVisible = true;
    },
    viewDetails(teacher) {
      this.selectedTeacher = teacher;
      this.detailDialogVisible = true;
    }
  }
};
</script>

<style scoped>
.el-table {
  overflow: auto; /* 允许表格内容滑动 */
}

.el-table .el-table__header th,
.el-table .el-table__body td {
  position: sticky; /* 使列固定 */
  background: white; /* 背景色 */
  z-index: 1; /* 确保在其他内容之上 */
}

.el-table .el-table__header th {
  top: 0; /* 固定在顶部 */
}

.el-table .el-table__body td {
  top: 0; /* 固定在顶部 */
}

/* 调整评分组件边距 */
.el-rate {
  margin-top: 8px;
}
</style>
